package com.hx.setting.dao.imp;

import com.hx.setting.dao.AbstractJDBCDaoSupport.ResultSetHandler;
import com.hx.setting.dao.BaseDao;
import com.hx.setting.dao.GroupsDao;
import com.hx.setting.dao.imp.JdbcBaseDaoImp;
import com.hx.setting.entity.GroupsEntity;
import com.hx.setting.entity.GroupsEntityWithBLOBs;
import com.hx.setting.model.Condition;
import com.hx.setting.model.GeneratedCriteria.Criterion;
import com.hx.setting.model.GeneratedCriteria;
import com.hx.setting.model.GroupsModel;
import com.hx.setting.model.Page;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Generate Date : 2014-03-16
 * Generate Version : v3.0
 * Generate Name : Honey v3.1 Test
 * Generate User : THINK
 * JDK Version : 1.5
 */
@SuppressWarnings("unchecked")
public class GroupsDaoImp<E extends GroupsEntity, PK extends Long> extends JdbcBaseDaoImp<E, PK> implements GroupsDao<E, PK>
{

	private static final String		TABLE_NAME			= "hx_groups";

	private static final String		PK_FIELD_NAME		= "id";

	private static final String		BASE_QUERY_FIELD	= "hx_groups.id AS id ,hx_groups.group_name AS groupName ";

	private static final String[]	BASE_FIELD			= new String[]
														{ "id", "groupName" };

	private static final String		BLOB_QUERY_FIELD	= "hx_groups.description AS description ";

	private static final String[]	BLOB_FIELD			= new String[]
														{ "description" };

	@Override
	protected String getTableName()
	{
		return TABLE_NAME;
	}

	@Override
	protected String getFieldPrimaryKeyName()
	{
		return PK_FIELD_NAME;
	}

	@Override
	protected ResultSetHandler getResultSetHandler()
	{
		return new GroupsDaoImp.QueryResultSetHandler();
	}

	@Override
	protected String[] getBaseField()
	{
		return BASE_FIELD;
	}

	@Override
	protected String[] getBlobField()
	{
		return BLOB_FIELD;
	}

	@Override
	protected String getBaseQueryField()
	{
		return BASE_QUERY_FIELD;
	}

	@Override
	protected String getBlobQueryField()
	{
		return BLOB_QUERY_FIELD;
	}

	@Override
	protected E getEntity(boolean hasBlob)
	{
		GroupsEntity answer = null;
		answer = hasBlob ? new GroupsEntityWithBLOBs() : new GroupsEntity();
		return (E) answer;
	}

	@Override
	public int insert(E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			StringBuilder sql = new StringBuilder( "INSERT INTO " ).append( TABLE_NAME );
			sql.append( "( group_name, description ) VALUES ( ?, ? )" );
			Object[] params = new Object[]
			{ bean.getGroupName(), bean instanceof GroupsEntityWithBLOBs ? ((GroupsEntityWithBLOBs) bean).getDescription() : null };
			try
			{
				GeneratedKeysHandler keyHandler = new GeneratedKeysHandler();
				answer = update( prepareConnection() ,true ,sql.toString() ,keyHandler ,params );
				bean.setId( keyHandler.getGeneratedKeys() );
			}
			catch ( SQLException e )
			{
				e.printStackTrace();
			}
		}
		return answer;
	}

	@Override
	public int insertSelective(E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			StringBuilder sql = new StringBuilder( "INSERT INTO " ).append( TABLE_NAME );
			StringBuilder field = new StringBuilder();
			StringBuilder value = new StringBuilder();
			List params = new ArrayList();
			Object o = null;
			o = bean.getGroupName();
			if ( o != null )
			{
				field.append( "group_name, " );
				value.append( "?, " );
				params.add( o );
			}
			o = bean instanceof GroupsEntityWithBLOBs ? ((GroupsEntityWithBLOBs) bean).getDescription() : null;
			if ( o != null )
			{
				field.append( "description, " );
				value.append( "?, " );
				params.add( o );
			}
			field.deleteCharAt( field.length() - 2 );
			sql.append( "( " ).append( field ).append( ") VALUES (" );
			value.deleteCharAt( value.length() - 2 );
			sql.append( value ).append( ')' );
			try
			{
				GeneratedKeysHandler keyHandler = new GeneratedKeysHandler();
				answer = update( prepareConnection() ,true ,sql.toString() ,keyHandler ,params.toArray() );
				bean.setId( keyHandler.getGeneratedKeys() );
			}
			catch ( SQLException e )
			{
				e.printStackTrace();
			}
		}
		return answer;
	}

	@Override
	public int update(E bean)
	{
		return updateByPrimaryKey( bean );
	}

	@Override
	public int updateByPrimaryKey(E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			String set = ("group_name=? ,description=?  ");
			answer = update( set ,"id=? " ,new Object[]
			{ bean.getGroupName(), bean instanceof GroupsEntityWithBLOBs ? ((GroupsEntityWithBLOBs) bean).getDescription() : null, bean.getId() } );
		}
		return answer;
	}

	public int updateByPrimaryKeySelective(E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			StringBuilder sql = new StringBuilder();
			StringBuilder field = new StringBuilder();
			StringBuilder finalCond = new StringBuilder();
			List params = new ArrayList();
			Object o = null;
			o = bean.getGroupName();
			if ( o != null )
			{
				field.append( "group_name=?, " );
				params.add( o );
			}
			o = bean instanceof GroupsEntityWithBLOBs ? ((GroupsEntityWithBLOBs) bean).getDescription() : null;
			if ( o != null )
			{
				field.append( "description=?, " );
				params.add( o );
			}
			field.deleteCharAt( field.length() - 2 );
			params.add( bean.getId() );
			answer = update( field.toString() ,"id=? " ,params.toArray() );
		}
		return answer;
	}

	@Override
	public int save(E bean)
	{
		return 0;
	}

	@Override
	public int updateByDynamic(Condition<? extends GeneratedCriteria> search, E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			String set = ("group_name=? ,description=? ");
			Object[] build = make( search );
			answer = update( set ,(String) build[0] ,com.hx.setting.utils.StringUtils.join( new Object[]
			{ bean.getGroupName(), bean instanceof GroupsEntityWithBLOBs ? ((GroupsEntityWithBLOBs) bean).getDescription() : null } ,(Object[]) build[1] ) );
		}
		return answer;
	}

	@Override
	public int updateByDynamicSelective(Condition<? extends GeneratedCriteria> search, E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			StringBuilder set = new StringBuilder();
			List params = new ArrayList();
			Object o = null;
			o = bean.getGroupName();
			if ( o != null )
			{
				set.append( "group_name=?, " );
				params.add( o );
			}
			o = bean instanceof GroupsEntityWithBLOBs ? ((GroupsEntityWithBLOBs) bean).getDescription() : null;
			if ( o != null )
			{
				set.append( "description=?, " );
				params.add( o );
			}
			set.deleteCharAt( set.length() - 2 );
			Object[] build = make( search );
			answer = update( set.toString() ,(String) build[0] ,com.hx.setting.utils.StringUtils.join( params.toArray() ,(Object[]) build[1] ) );
		}
		return answer;
	}

	private class QueryResultSetHandler implements ResultSetHandler
	{

		@Override
		public GroupsEntity handle(ResultSet resultSet, boolean hasBlob) throws SQLException
		{
			GroupsEntity bean = null;
			if ( resultSet.next() )
			{
				bean = hasBlob ? new GroupsEntityWithBLOBs() : new GroupsEntity();
				Object obj = null;
				obj = resultSet.getLong( "id" );
				if ( obj != null && !resultSet.wasNull() ) bean.setId( (Long) obj );
				obj = resultSet.getString( "groupName" );
				if ( obj != null && !resultSet.wasNull() ) bean.setGroupName( (String) obj );
				if ( hasBlob )
				{
					GroupsEntityWithBLOBs b = (GroupsEntityWithBLOBs) bean;
					b.setDescription( resultSet.getString( "description" ) );
				}
			}
			return bean;
		}
	}
}
/* Honey v3.1 Test - version:4 - OriginalChecksum:Bf5eec33d9377301cc55a8fe7f7721dce (do not edit this line) */
